// -*- C++ -*-

// Copyright 2006-2008 Deutsches Forschungszentrum fuer Kuenstliche Intelligenz
// or its licensors, as applicable.
//
// You may not use this file except under the terms of the accompanying license.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you
// may not use this file except in compliance with the License. You may
// obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Project: OCRopus
// File: main-seg-eval.cc
// Purpose: Color based segmentation evaluation using graph matching
// Responsible: Faisal Shafait (faisal.shafait@dfki.de) and Syed Saqib Bukhari (bukhari@iupr.com)
// Reviewer:
// Primary Repository:
// Web Sites: www.iupr.org, www.dfki.de

#include "colib/colib.h"
#include "ocropus.h"
#include "seg-eval.h"
using namespace ocropus;
using namespace colib;
using namespace iulib;

#define DONTCARE   0xffffffff
#define NOISE      0x00000000
#define BACKGROUND 0x00ffffff
#define ZONEBOXES 0
#define LINEBOXES 1
#define WORDBOXES 2

int max(intarray &image) {
    int mv = image[0];
    for(int i=1;i<image.length();i++) mv = max(mv,image[i]);
    return mv;
}

int min(intarray &image) {
    int mv = image[0];
    for(int i=1;i<image.length();i++) mv = min(mv,image[i]);
    return mv;
}

int read_normalized(intarray &image,char *file,int *noiseval,bool allow_dont_care=false) {
  //printf("Read_normalized started..%s\n",file);

    read_png_packed(image,fopen(file,"r"));

    Enumerator enumerator(1);
    for(int i=0;i<image.length();i++) {
	int value = image[i];
	if(value == BACKGROUND) {image[i]=0; continue;}
	if(value==DONTCARE && allow_dont_care)
	    image[i] = -1; 
	else
	    image[i] = enumerator[value];
	if(value == NOISE)
            *noiseval = image[i];
    }
    //fprintf(stderr,"%s: %d\n",file,enumerator.total());
    return enumerator.total();
}
int main(int argc,char **argv) {
    try {
        if(argc != 3) {
            fprintf(stderr,"usage: ... input-1 input-2\n");
            exit(1);
        }
		intarray model,image,fmodel,fimage;
    	int mcolors,icolors,mnoiseval=-1,inoiseval=-1;
   	 	mcolors = read_normalized(model,argv[1],&mnoiseval);
    	icolors = read_normalized(image,argv[2],&inoiseval);
        //intarray mimage;
        //read_png_packed(mimage,stdio(argv[1],"r"),true);
		//intarray mimage;
		//copy(mimage,_mimage);
		//intarray iimage;
        //read_png_packed(iimage,stdio(argv[2],"r"),true);
		//intarray iimage;	
		//copy(iimage,_iimage);
		Evaluator eval;
		//intarray fmimage;
        //read_png(_fmimage,stdio(argv[3],"r"),true);
		//intarray fmimage;
		//copy(fmimage,_fmimage);
		//intarray fiimage;
        //read_png(_fiimage,stdio(argv[4],"r"),true);
		//intarray fiimage;
		//copy(fiimage,_fiimage);
		//printf("\n Hello");
		int num_over,num_under,num_miss,num_falarm;
		int abc = segeval_full(model,image,eval,fmodel,fimage,mnoiseval,inoiseval,num_over,num_under,num_miss,num_falarm,100,0.1);
		//printf("\n %d %d %d %d",num_over,num_under,num_miss,num_falarm);
    }
    catch(const char *oops) {
        fprintf(stderr,"oops: %s\n",oops);
    }
}

